<template>
  <div class="wrapper">
    <transition
      appear
      name="animate__animated animate__bounce"
      enter-active-class="animate__fadeInRight"
      leave-active-class="animate__fadeOutLeft"
    >
      <el-card class="card">
        <h2 style="text-align: center">登录页面</h2>

        <el-form
          :rules="rules"
          status-icon
          :model="loginForm"
          aria-autocomplete="off"
          ref="loginForm"
        >
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="loginForm.email"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="loginForm.password"
              auto-complete="new-password"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              style="width: 80%"
              @click="submitForm('loginForm')"
              >登录</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-right"
              circle
              @click="goRegister"
            ></el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </transition>
  </div>
</template>

<script>
import "animate.css";
import jwt_decode from 'jwt-decode'
export default {
  name: "Login",
  data () {
    return {
      loginForm: {
        email: "",
        password: "",
      },
      rules: {
        email: [{ required: true, message: "邮箱格式不正确", type: 'email', trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },

  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/api/users/login', this.loginForm)
            .then(res => {
              this.$message.success('登陆成功')
              const { token } = res.data
              //存储到localStorage
              localStorage.setItem('eleToken', token)
              //解析token
              const decode = jwt_decode(token)
              console.log(decode)
              //token存储到vuex中
              this.$store.dispatch('setAuthenticated', !this.isEmpty(decode))
              this.$store.dispatch('setUser', decode)
              this.$router.push('/index')
            })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    isEmpty (value) {
      return (
        value === undefined || value === null ||
        (typeof value === 'object' && Object.keys(value).length === 0) ||
        (typeof value === 'string' && value.trim().length === 0)
      )
    },
    goRegister () {
      this.$router.push("/register");
    },
  },
};
</script>

<style scoped>
.wrapper {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.card {
  width: 400px;
}
</style>